//
//  YiJianFanKuiViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YiJianFanKuiViewController.h"

@interface YiJianFanKuiViewController ()<UITextViewDelegate>

@property (nonatomic, strong)UITextView *yijianTextView;

@property (nonatomic, strong)UILabel *textViewPlaceHo;

@property (nonatomic, strong)UIButton *commiteBtn;

@end

@implementation YiJianFanKuiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"意见反馈";
    
    [self setUI];
    
}

-(void)setUI{
    _yijianTextView = [[UITextView alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 40 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 40 * KscreenWidth, 200 *KscreenHeight)];
    _yijianTextView.layer.borderColor = [hui2Color CGColor];
    _yijianTextView.layer.borderWidth = 1;
    _yijianTextView.layer.cornerRadius = 6;
    _yijianTextView.font = [UIFont systemFontOfSize:15 * KscreenWidth];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _yijianTextView.delegate = self;
    _yijianTextView.layer.masksToBounds = YES;
    
    _textViewPlaceHo = [[UILabel alloc]initWithFrame:CGRectMake(5 * KscreenWidth,7 * KscreenHeight, 200 * KscreenWidth, 20 * KscreenHeight)];
    _textViewPlaceHo.text = @"请输入您的意见";
    _textViewPlaceHo.textColor = hui2Color;
    _textViewPlaceHo.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    [_yijianTextView addSubview:_textViewPlaceHo];
    
    [self.view addSubview:_yijianTextView];
    
    
    _commiteBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _commiteBtn.frame = CGRectMake(20 * KscreenWidth, 400 * KscreenHeight, ScreenWidth - 40 * KscreenWidth, 50 * KscreenHeight);
    _commiteBtn.backgroundColor = ZhuTiColor;
    [_commiteBtn setTitle:@"提交" forState:0];
    [_commiteBtn addTarget:self action:@selector(commiteAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_commiteBtn];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_yijianTextView resignFirstResponder];
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];//按回车取消第一相应者
    }
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    self.textViewPlaceHo.hidden = YES;
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{//将要停止编辑(不是第一响应者时)
    if (textView.text.length == 0) {
        self.textViewPlaceHo.hidden = NO;
    }
    return YES;
}

//提交
-(void)commiteAction{
    [_yijianTextView resignFirstResponder];
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       @"content":_yijianTextView.text
                       };
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Ucenter&a=sendOpinion",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
         
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"" message:responseObject[@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil, nil];
            [alertView show];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
